#!/bin/bash
# Author: yanjingtu <yanjingtu2008@yeah.net>
# Date: 2020/7/24
# Url: https://gitee.com/yanjingtu/centos-dockerfiles/tree/master/scripts/

mkdir /opt/app-root
cd /opt/app-root

# 首先尝试从指定地址下载程序包
curl -m 5 http://7.7.9.9/tools/linux/autotest/autotest_platform.tgz | tar -zx
if [ $? -ne 0 ]; then
    echo "下载本地的包失败，安装git"
    yum install -q -y --nogpgcheck git && yum clean all
    echo "从gitee下载源码包"
    git clone https://gitee.com/yanjingtu/autotest_platform.git
    if [ $? -ne 0 ]; then
        echo "下载源码包失败，安装终止"
        exit 1
    fi
fi

mv autotest_platform src
cd src
echo "安装python依赖包"
pip3 install -r requirements.txt
cp app/dockerfile/runall.sh /
chmod +x /runall.sh

# 安装mysql
echo "安装mysql服务端和客户端"
yum install -y -q mariadb-server mariadb && yum clean all

# 配置并启动mysql
echo "初始化mysql系统表"
mysql_install_db > /dev/null
echo "启动mysqld服务端"
/usr/libexec/mysqld -u root 2> /dev/null &
sleep 4
echo "设置数据库用户密码并创建数据库"
mysqladmin -u root password 'my@pass#123'
mysql -p'my@pass#123' -e "create database test_auto_new"

# 修改程序的配置（app/config.py）
sed -i 's/yourpassword/my@pass#123/' app/config.py

# 创建数据表
echo "创建业务表"
mysql -p'my@pass#123' < init.sql

pkill mysqld
sed -i 's:pwd:/usr/libexec/mysqld -u root \&:' /runall.sh
echo "安装完成"
